package gui.mainFrame;

import gui.GUIController;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import testGenerator.Controller;

/**
 * Provides the menu bar.
 * @author William Whitney
 */
public class MenuBar extends JMenuBar
{
    private static final long serialVersionUID = 1L;

    private final GUIController guiController;
    private JMenuItem testHeaderBtn;
    private JMenuItem testSetupBtn;
    private JMenuItem testTearDown;
    private Controller controller;

    /**
     * Default constructor.
     * @param guiController
     * @param controller  
     */
    public MenuBar(GUIController guiController, Controller controller)
    {
        this.guiController = guiController;
        this.controller = controller;

        //Add File Menu
        this.add(getFileMenu());

        //Add Edit Menu
        this.add(getEditMenu());

        //Add Help Menu
        this.add(getHelpMenu_());
    }

    /**
     * Adds the file menu.
     */
    private JMenu getFileMenu()
    {
        JMenu fileMenu = new JMenu("File");

        //Add new Btn
        JMenuItem newTestBtn = new JMenuItem("New Test");
        newTestBtn.addActionListener(newBtnListener());
        fileMenu.add(newTestBtn);
        
        JMenuItem projectConfigurationBtn = new JMenuItem("Configure Project");
        projectConfigurationBtn.addActionListener(projectConfigurationBtnListener());
        fileMenu.add(projectConfigurationBtn);

        //Exit Btn
        JMenuItem exitBtn = new JMenuItem("Exit");
        exitBtn.addActionListener(fileBtnListener());
        fileMenu.add(exitBtn);

        return fileMenu;
    }

    /**
     * Adds an edit menu.
     */
    private JMenu getEditMenu()
    {
        JMenu editMenu = new JMenu("Edit");

        //Add test header
        testHeaderBtn = new JMenuItem("Test Header");
        testHeaderBtn.addActionListener(getTestHeaderActionListener());
        editMenu.add(testHeaderBtn);

        //Add new Btn
        testSetupBtn = new JMenuItem("Test Setup");
        testSetupBtn.addActionListener(getTestSetupActionListener());
        editMenu.add(testSetupBtn);

        //Exit Btn
        testTearDown = new JMenuItem("Test Tear Down");
        testTearDown.addActionListener(getTestTearDownActionListener());
        editMenu.add(testTearDown);

        return editMenu;
    }

    /**
     * Add the help menu.
     */
    private JMenu getHelpMenu_()
    {
        JMenu helpMenu = new JMenu("Help");

        //User Guide Button
        JMenuItem userGuide = new JMenuItem("User Guide");
        userGuide.addActionListener(getViewUserGuideListener());
        helpMenu.add(userGuide);

        //About Button
        JMenuItem aboutButton = new JMenuItem("About");
        aboutButton.addActionListener(getAboutButtonListener());
        helpMenu.add(aboutButton);

        return helpMenu;
    }

    /**
     * Provides a listener for when the new project button is pressed.
     * @return
     */
    private ActionListener newBtnListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                guiController.showNewTestWizard();
            }
        };
    }
    
    private ActionListener projectConfigurationBtnListener()
    {
        return new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiController.showProjectConfiguration();
            }  
        };
    }

    /**
     * Provides a listener for when the exit button is pressed.
     * @return
     */
    private ActionListener fileBtnListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        };
    }

    /**
     * Returns an action listener for the about button
     * on the menu.
     * @return ActionListener
     */
    public ActionListener getAboutButtonListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                guiController.showAboutBox();

            }
        };
    }

    /**
     * Returns an action listener for the view user guide button.
     * @return ActionListener
     */
    public ActionListener getViewUserGuideListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                guiController.showUserGuide();
            }
        };
    }

    public ActionListener getTestSetupActionListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                guiController.showTestSetup();
            }
        };
    }

    public ActionListener getTestTearDownActionListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                guiController.showTestTeardown();
            }
        };
    }

    public ActionListener getTestHeaderActionListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                guiController.showTestHeader();
            }
        };
    }

    public void disableEditMenuOptions(boolean val)
    {
        this.testSetupBtn.setEnabled(!val);
        this.testTearDown.setEnabled(!val);
        this.testHeaderBtn.setEnabled(!val);
    }
}
